package ru.bmstu.vks.client.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class FileTransferingProgressDialog extends JDialog {

	private static final long serialVersionUID = 5815894404678869674L;
	private final JPanel contentPanel = new JPanel();
	private JProgressBar transferProgress;
	private JLabel transfered;
	private JLabel speed;
	private JLabel receivedLbl;
	private JLabel speedLbl;
	private JLabel timeLbl;
	private JLabel time;
	private JLabel meanSpeed;
	private JPanel meanSpeedPanel;
	private JLabel process;
	private JLabel meanSpeedCompute;
	private JLabel meanSpeedLabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			FileTransferingProgressDialog dialog = new FileTransferingProgressDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public FileTransferingProgressDialog() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(FileTransferingProgressDialog.class.getResource("/data/icon.png")));
		setResizable(false);
		setTitle("\u041F\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0444\u0430\u0439\u043B\u0430:  ");
		setBounds(100, 100, 500, 243);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		receivedLbl = new JLabel("\u041F\u0435\u0440\u0435\u0434\u0430\u043D\u043E \u0431\u0430\u0439\u0442: ");
		receivedLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		receivedLbl.setBounds(10, 45, 126, 14);
		contentPanel.add(receivedLbl);
		
		transfered = new JLabel("\u0431\u0430\u0439\u0442\u044B");
		transfered.setFont(new Font("Tahoma", Font.BOLD, 18));
		transfered.setBounds(198, 36, 286, 29);
		contentPanel.add(transfered);
		
		speedLbl = new JLabel("\u0421\u043A\u043E\u0440\u043E\u0441\u0442\u044C \u043F\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043D\u043D\u044B\u0445:");
		speedLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		speedLbl.setBounds(10, 86, 190, 14);
		contentPanel.add(speedLbl);
		
		speed = new JLabel("\u0441\u043A\u043E\u0440\u043E\u0441\u0442\u044C");
		speed.setFont(new Font("Tahoma", Font.BOLD, 18));
		speed.setBounds(198, 77, 286, 29);
		contentPanel.add(speed);
		
		timeLbl = new JLabel("\u0412\u0440\u0435\u043C\u044F:");
		timeLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		timeLbl.setBounds(10, 127, 190, 14);
		contentPanel.add(timeLbl);
		
		time = new JLabel("\u0432\u0440\u0435\u043C\u044F");
		time.setFont(new Font("Tahoma", Font.BOLD, 18));
		time.setBounds(198, 118, 286, 29);
		contentPanel.add(time);
		{
			transferProgress = new JProgressBar();
			transferProgress.setBackground(new Color(255, 255, 255));
			transferProgress.setBounds(10, 5, 474, 29);
			contentPanel.add(transferProgress);
		}
		
		meanSpeedPanel = new JPanel();
		meanSpeedPanel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		meanSpeedPanel.setBounds(0, 154, 494, 58);
		contentPanel.add(meanSpeedPanel);
		meanSpeedPanel.setLayout(null);
		
		meanSpeedLabel = new JLabel("\u0421\u0440\u0435\u0434\u043D\u044F\u044F \u0441\u043A\u043E\u0440\u043E\u0441\u0442\u044C \u043F\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043D\u043D\u044B\u0445:");
		meanSpeedLabel.setBounds(28, 16, 218, 16);
		meanSpeedLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		meanSpeedLabel.setVisible(false);
		meanSpeedPanel.add(meanSpeedLabel);
		
		meanSpeed = new JLabel("\u0441\u043A\u043E\u0440\u043E\u0441\u0442\u044C");
		meanSpeed.setBounds(256, 11, 228, 22);
		meanSpeed.setForeground(new Color(0, 102, 0));
		meanSpeed.setFont(new Font("Tahoma", Font.BOLD, 18));
		meanSpeed.setVisible(false);
		meanSpeedPanel.add(meanSpeed);
		
		process = new JLabel("");
		process.setIcon(new ImageIcon(FileTransferingProgressDialog.class.getResource("/data/process.gif")));
		process.setBounds(10, 0, 102, 60);
		meanSpeedPanel.add(process);
		
		meanSpeedCompute = new JLabel("\u0412\u044B\u0447\u0438\u0441\u043B\u044F\u0435\u0442\u0441\u044F \u0441\u0440\u0435\u0434\u043D\u044F\u044F \u0441\u043A\u043E\u0440\u043E\u0441\u0442\u044C...");
		meanSpeedCompute.setBounds(94, 25, 390, 14);
		meanSpeedPanel.add(meanSpeedCompute);
	}
	
	public void init (String fileName, long fileSize, int bufferSize) {
		FileTransferingProgressDialog.this.setTitle(FileTransferingProgressDialog.this.getTitle() + " " + fileName);
	}
	
	public void changeProgress(int value, long transferedBytes, double speed, long time) {
		this.transferProgress.setValue(value);
		this.transfered.setText(String.valueOf(transferedBytes));
		this.speed.setText(String.valueOf(speed) + " Kb/s");
		int hours = (int)(time/3600000) % 60;
		int minutes = (int)(time/60000) % 60;
		int seconds = (int)(time/1000) % 60;
		
		this.time.setText("" + hours + ":" + minutes + ":" + seconds);
	}
	
	public void setMeanSpead(double speed) {
		this.meanSpeedPanel.setVisible(true);
		this.meanSpeed.setVisible(true);
		this.meanSpeedLabel.setVisible(true);
		this.meanSpeedCompute.setVisible(false);
		this.process.setVisible(false);
		this.meanSpeed.setText(String.valueOf(speed) + " Kb/s");
		this.transferProgress.setValue(100);
	}
}
